﻿hpxAdminApp.controller('selectBackgroundInformationController', function (exportService, $interval, $rootScope, $scope, $stateParams, $state, API_URL, NgTableParams, constantsService, addressService, customerService, payService, messageService) {
    console.log($scope)
    console.log($rootScope)

    var emptyEntity = {};
    var newEntity = angular.copy(emptyEntity, newEntity);

    $scope.filter = {
        'billTypeID': 0,        //默认未审核
        tradeTypeCode: '',
        billTypeID: '',
        billTys:'',
    };
    $scope.model = {
        product_location_province_id:''
    }
    

    var timer = $interval($scope.reflash, 60 * 1000);
    $scope.$on(
        "$destroy",
        function (event) {
            $interval.cancel(timer);
        }
    );

    //获取所有的省级地址
    addressService.queryAll().then(function (data) {
        $scope.provinceData = data;
        $scope.provinceChange();
    });
    $scope.provinceChange = function () {
        if (!$scope.model.product_location_province_id) {
            $scope.cityData = [];
        } else if ($scope.model.product_location_province_id == 1 || $scope.model.product_location_province_id == 20 || $scope.model.product_location_province_id == 860 || $scope.model.product_location_province_id == 2462) {
            $scope.filter.tradeProvinceId = $scope.model.product_location_province_id + 1;
            return addressService.queryCity($scope.filter.tradeProvinceId).then(function (data) {
                $scope.cityData = data;
            });
        } else {
            return addressService.queryCity($scope.model.product_location_province_id).then(function (data) {
                $scope.cityData = data;
            });
        }

    }

    $scope.filterCityChange = function () {
        $scope.filter.locationId = $scope.filter.CityID;
        //$scope.tableParams.reload();
    }
    

    $scope.filter = {
        'deadlineTime1': '',     // 交易起始时间
        'deadlineTime2': '',     // 交易终止时间
        'deadlineTime3': '',     // 注册起始时间
        'deadlineTime4': '',     // 注册终止时间
        'deadlineTime5': ''      // 选择发送时间
    };

    $("#start_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#start_time").datetimepicker("setEndDate", $("#end_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.deadlineTime1 = $("#start_time").val();
    });

    $("#end_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#end_time").datetimepicker("setStartDate", $("#start_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.deadlineTime2 = $("#end_time").val();
    });

    $("#hstart_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#hstart_time").datetimepicker("setEndDate", $("#hend_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.deadlineTime3 = $("#hstart_time").val();
    });
    $("#hend_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#hend_time").datetimepicker("setStartDate", $("#hstart_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.deadlineTime4 = $("#hend_time").val();
    });
    $("#hsend_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#hsend_time").datetimepicker("setStartDate", $("#hsend_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.deadlineTime5 = $("#hsend_time").val();
    });
    // 查询所有
    $scope.hpxFind = {
        'person': 0,
        'personLevelId': 0,
        'htraderLevelCode': 1,
        'hbillTypeId': 101,
        'keyworld': "",
        'locationld': ""
    }

    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }

    $scope.hpxFindS = function () {
        $scope.hpxFind.person = $("#h_Form").find("input[name='customerTypeCode']:checked").val();
        $scope.hpxFind.personLevelId = $("#h_Form").find("input[name='customerLevelId']:checked").val();
        $scope.hpxFind.htraderLevelCode = $("#h_Form").find("input[name='traderLevelCode']:checked").val();
        $scope.hpxFind.hbillTypeId = $("#h_Form").find("input[name='billTypeId']:checked").val();

        $scope.tableParams = new NgTableParams({ 'sorting': { 'id': 'asc' } }, {
            getData: function (params) {
                $scope.hpxFind.person = $("#h_Form").find("input[name='customerTypeCode']:checked").val();
                $scope.hpxFind.personLevelId = $("#h_Form").find("input[name='customerLevelId']:checked").val();
                $scope.hpxFind.htraderLevelCode = $("#h_Form").find("input[name='traderLevelCode']:checked").val();
                $scope.hpxFind.hbillTypeId = $("#h_Form").find("input[name='billTypeId']:checked").val();

                return messageService.findAllMessage(params, $scope.hpxFind.keyworld, $scope.hpxFind.locationld, $scope.hpxFind.person, $scope.hpxFind.personLevelId, $scope.hpxFind.htraderLevelCode, $scope.hpxFind.hbillTypeId, $scope.filter.deadlineTime3, $scope.filter.deadlineTime4, $scope.filter.deadlineTime1, $scope.filter.deadlineTime2).then(function (data) {
                    $scope.first = $scope.getFirst(params);
                    console.log(data)
                    $scope.hpxMessageA = data;
                    var hpxList = new Array();
                    angular.forEach(data, function (ele, index) {
                        hpxList.push(ele.id);
                    })
                    $scope.hpxCustomer = hpxList;
                    return data;
                });
            }
        });

        
    }

    // 发送
    $scope.hpxSend = function () {

        var notification_type = $("#h_Form").find("input[name='notification_type']:checked").val() || 1;
        var news_type = $("#h_Form").find("input[name='news_type']:checked").val() || 1;
        var hpxTimes = $scope.filter.deadlineTime5.valueOf();
        //var hpxss = hpxTimes.valueOf();
        //console.log(hpxTimes)
        //var ti = $scope.model.dates;
        //console.log(ti)
        //console.log("消息类型" + notification_type)
        //console.log("发送类型" + news_type)
        //console.log("发送消息的标题" + $scope.model.notification_title)
        //console.log("发送时间" + $scope.filter.deadlineTime5)

        messageService.sendMessage({
            'notification_title': $scope.model.notification_title,
            'notification_type': notification_type,
            'send_time': $scope.filter.deadlineTime5,
            'news_type': news_type,
            'activity_period': $scope.model.activity_period,
            'activity_name': $scope.model.activity_name,
            'things': $scope.model.things,
            'ids': $scope.hpxCustomer
        }).then(function (data) {
            console.log(data)
            confirm("消息发送成功。")
        })
    }
    
    

    //导出表格
    $scope.exportExcel = function () {
        var resource_url = API_URL + "/customers/customerReview?n=65535&orderBy=%2Bid&p=1";
        var sheet_name = "企业审核表";
        var label_names, label_types, label_keys;
        if ($scope.filter.checkedType == 1) {
            resource_url += "&isChecked=1";
            sheet_name += "_已审核";
        } else if ($scope.filter.checkedType == 0) {
            resource_url += "&isChecked=0";
            sheet_name += "_未审核";
        }
        label_names = ["企业名称", "统一社会信用码/营业执照注册号", "联系人", "联系号码"];
        label_types = ["String", "String", "String", "String"];
        label_keys = ["enterprise_name", "credential_number", "contact_person", "contact_phone"];

        var excelRequest = {
            "resource_url": resource_url,
            "resource_name": "enterprises",
            "sheet_name": sheet_name,
            "label_names": label_names,
            "label_types": label_types,
            "label_keys": label_keys
        };
        var token = '';
        if ($rootScope.identity != undefined) {
            token = $rootScope.identity.token;
        }
        exportService.exportExcel('export', token, excelRequest).then(function (result) {
            var newWin = window.open('loading page');
            newWin.location.href = result.data.data;
        })
    }
    
});
